package com.muyi.biling.di;

import android.app.Application;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import autodagger.AutoExpose;
import com.muyi.biling.MyApp;
import com.muyi.biling.SharedPreferencesHelper;
import com.muyi.biling.rest.Api;
import com.muyi.biling.rest.ApiHelper;
import com.muyi.biling.rest.CustomTrust;
import com.muyi.biling.rest.UnSafeHostnameVerifier;
import com.muyi.biling.util.Constants;
import com.muyi.biling.util.Utils;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by developer on 18-1-24.
 */

@Module
public class AppModule {

    private Application mApp;

    public AppModule(Application app) {
        mApp = app;
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    SharedPreferencesHelper provideSharedPreferences() {
        return new SharedPreferencesHelper(mApp);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    Api provideApi(CustomTrust customTrust, UnSafeHostnameVerifier hostnameVerifier) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
//                .addNetworkInterceptor(new StethoInterceptor());

        httpClientBuilder
                // 连接超时时间设置
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                // 读取超时时间设置
                .readTimeout(30, TimeUnit.SECONDS)
                // 失败重试
                .retryOnConnectionFailure(true)
                // TODO uncomment it
                // 查看http请求内容
                .addInterceptor(httpLoggingInterceptor)
//                .addInterceptor(httpInterceptor)
        ;

        // 添加https
        httpClientBuilder
                .sslSocketFactory(customTrust.sslSocketFactory, customTrust.trustManager)
                .hostnameVerifier(hostnameVerifier);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.API.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(httpClientBuilder.build())
                .build();

        return retrofit.create(Api.class);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    ApiHelper provideApiHelper(Api api, SharedPreferencesHelper helper) {
        String devId = Utils.getDeviceUniqueId(mApp);
        return new ApiHelper(api, devId, helper);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    UnSafeHostnameVerifier provideUnSafeHostnameVerifier() {
        return new UnSafeHostnameVerifier();
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    CustomTrust provideCustomTrust() {
        return new CustomTrust(mApp);
    }


}
